package com.rfid.sockethelper;

import java.util.ArrayList;
import java.util.List;

public class ClientManager implements SocketBase.OnReceiveListener {

    List<ClientSocketHelper> mClientSocketHelpers;
    static ClientManager manager;
    private List<SocketBase.OnReceiveListener> mOnReceiveListeners;

    public static final int STATUS_NORMAL = 0;
    public static final int STATUS_CONNECTED = 1;
    public static final int STATUS_CONNECTING = 2;

    private ClientManager() {
        mOnReceiveListeners = new ArrayList<>();
        mClientSocketHelpers = new ArrayList<>();
    }

    public static ClientManager getManager(){
        if (manager == null) {
            manager = new ClientManager();
        }
        return manager;
    }

    public void postConnect(String ip) {
        ClientSocketHelper clientSocketHelper = null;
        for (ClientSocketHelper helper : mClientSocketHelpers) {
            if (ip.equals(helper.getServerIP())) {
                helper.clear();
                mClientSocketHelpers.remove(helper);
                clientSocketHelper = new ClientSocketHelper(this);
                break;
            }
        }
        if (clientSocketHelper == null) {
            clientSocketHelper = new ClientSocketHelper(this);
        }
        mClientSocketHelpers.add(clientSocketHelper);
        clientSocketHelper.postConnectToServer(ip);
        System.out.println("client size = " + mClientSocketHelpers.size());
    }

    public void addListener(SocketBase.OnReceiveListener listener) {
        System.out.println("setListener = " + (listener == null?"null":listener.getClass().getName()));
        if (!mOnReceiveListeners.contains(listener)) {
            mOnReceiveListeners.add(listener);
        }
    }

    public void removeListener(SocketBase.OnReceiveListener listener) {
        mOnReceiveListeners.remove(listener);
    }

    @Override
    public void onReceived(String ip, short cmd, CharSequence... text) {
        for (SocketBase.OnReceiveListener listener:mOnReceiveListeners) {
            listener.onReceived(ip,cmd,text);
        }
    }

    @Override
    public void onConnect(String ip, boolean connect) {
        System.out.println("onConnect " + ip);
        if (!connect) {
            for (ClientSocketHelper helper : mClientSocketHelpers) {
                if (helper.getServerIP().equals(ip)) {
                    helper.clear();
                    mClientSocketHelpers.remove(helper);
                    break;
                }
            }
        }
        for (SocketBase.OnReceiveListener listener:mOnReceiveListeners) {
            listener.onConnect(ip,connect);
        }
    }

//    @Override
//    public void onConfig(String ip, String name, String text) {
//        for (SocketBase.OnReceiveListener listener:mOnReceiveListeners) {
//            listener.onConfig(ip,name,text);
//        }
//    }

    public void destroy() {
        for (ClientSocketHelper helper : mClientSocketHelpers) {
            helper.clear();
        }
        mClientSocketHelpers.clear();
        mOnReceiveListeners.clear();
    }

    public int getStatus(String ip) {
        for (ClientSocketHelper helper : mClientSocketHelpers) {
            if (helper.getServerIP().equals(ip)) {
                if (helper.isConnecting())
                    return STATUS_CONNECTING;
                if (helper.isConnect()) {
                    return STATUS_CONNECTED;
                }
                return STATUS_NORMAL;
            }
        }
        return STATUS_NORMAL;
    }

    public ClientSocketHelper getClient(String ip) {
        System.out.println("getClient client size = " + mClientSocketHelpers.size());
        for (ClientSocketHelper helper : mClientSocketHelpers) {
            if (helper.getServerIP().equals(ip)) {
                return helper;
            }
        }
        return null;
    }

    public void clear(String ip) {
        for (ClientSocketHelper helper : mClientSocketHelpers) {
            if (helper.getServerIP().equals(ip)) {
                mClientSocketHelpers.remove(helper);
                helper.clear();
                break;
            }
        }
    }
}
